home *** CD-ROM | disk | FTP | other *** search
/ Enter 2006 September / Enter 09 2006.iso / Internet / SpamExperts Home 1.1 / SpamExperts Home.exe / lib / spamexperts.modules / os2emxpath.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-07-14  |  10.1 KB  |  429 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Common pathname manipulations, OS/2 EMX version.
  5.  
  6. Instead of importing this module directly, import os and refer to this
  7. module as os.path.
  8. '''
  9. import os
  10. import stat
  11. __all__ = [
  12.     'normcase',
  13.     'isabs',
  14.     'join',
  15.     'splitdrive',
  16.     'split',
  17.     'splitext',
  18.     'basename',
  19.     'dirname',
  20.     'commonprefix',
  21.     'getsize',
  22.     'getmtime',
  23.     'getatime',
  24.     'getctime',
  25.     'islink',
  26.     'exists',
  27.     'lexists',
  28.     'isdir',
  29.     'isfile',
  30.     'ismount',
  31.     'walk',
  32.     'expanduser',
  33.     'expandvars',
  34.     'normpath',
  35.     'abspath',
  36.     'splitunc',
  37.     'curdir',
  38.     'pardir',
  39.     'sep',
  40.     'pathsep',
  41.     'defpath',
  42.     'altsep',
  43.     'extsep',
  44.     'devnull',
  45.     'realpath',
  46.     'supports_unicode_filenames']
  47. curdir = '.'
  48. pardir = '..'
  49. extsep = '.'
  50. sep = '/'
  51. altsep = '\\'
  52. pathsep = ';'
  53. defpath = '.;C:\\bin'
  54. devnull = 'nul'
  55.  
  56. def normcase(s):
  57.     '''Normalize case of pathname.
  58.  
  59.     Makes all characters lowercase and all altseps into seps.'''
  60.     return s.replace('\\', '/').lower()
  61.  
  62.  
  63. def isabs(s):
  64.     '''Test whether a path is absolute'''
  65.     s = splitdrive(s)[1]
  66.     if s != '':
  67.         pass
  68.     return s[:1] in '/\\'
  69.  
  70.  
  71. def join(a, *p):
  72.     '''Join two or more pathname components, inserting sep as needed'''
  73.     path = a
  74.     for b in p:
  75.         if isabs(b):
  76.             path = b
  77.             continue
  78.         if path == '' or path[-1:] in '/\\:':
  79.             path = path + b
  80.             continue
  81.         path = path + '/' + b
  82.     
  83.     return path
  84.  
  85.  
  86. def splitdrive(p):
  87.     '''Split a pathname into drive and path specifiers. Returns a 2-tuple
  88. "(drive,path)";  either part may be empty'''
  89.     if p[1:2] == ':':
  90.         return (p[0:2], p[2:])
  91.     
  92.     return ('', p)
  93.  
  94.  
  95. def splitunc(p):
  96.     """Split a pathname into UNC mount point and relative path specifiers.
  97.  
  98.     Return a 2-tuple (unc, rest); either part may be empty.
  99.     If unc is not empty, it has the form '//host/mount' (or similar
  100.     using backslashes).  unc+rest is always the input path.
  101.     Paths containing drive letters never have an UNC part.
  102.     """
  103.     if p[1:2] == ':':
  104.         return ('', p)
  105.     
  106.     firstTwo = p[0:2]
  107.     if firstTwo == '/' * 2 or firstTwo == '\\' * 2:
  108.         normp = normcase(p)
  109.         index = normp.find('/', 2)
  110.         if index == -1:
  111.             return ('', p)
  112.         
  113.         index = normp.find('/', index + 1)
  114.         if index == -1:
  115.             index = len(p)
  116.         
  117.         return (p[:index], p[index:])
  118.     
  119.     return ('', p)
  120.  
  121.  
  122. def split(p):
  123.     '''Split a pathname.
  124.  
  125.     Return tuple (head, tail) where tail is everything after the final slash.
  126.     Either part may be empty.'''
  127.     (d, p) = splitdrive(p)
  128.     i = len(p)
  129.     while i and p[i - 1] not in '/\\':
  130.         i = i - 1
  131.     head = p[:i]
  132.     tail = p[i:]
  133.     head2 = head
  134.     while head2 and head2[-1] in '/\\':
  135.         head2 = head2[:-1]
  136.     if not head2:
  137.         pass
  138.     head = head
  139.     return (d + head, tail)
  140.  
  141.  
  142. def splitext(p):
  143.     '''Split the extension from a pathname.
  144.  
  145.     Extension is everything from the last dot to the end.
  146.     Return (root, ext), either part may be empty.'''
  147.     (root, ext) = ('', '')
  148.     for c in p:
  149.         if c in [
  150.             '/',
  151.             '\\']:
  152.             root = root + ext + c
  153.             ext = ''
  154.             continue
  155.         if c == '.':
  156.             if ext:
  157.                 root = root + ext
  158.                 ext = c
  159.             else:
  160.                 ext = c
  161.         ext
  162.         if ext:
  163.             ext = ext + c
  164.             continue
  165.         root = root + c
  166.     
  167.     return (root, ext)
  168.  
  169.  
  170. def basename(p):
  171.     '''Returns the final component of a pathname'''
  172.     return split(p)[1]
  173.  
  174.  
  175. def dirname(p):
  176.     '''Returns the directory component of a pathname'''
  177.     return split(p)[0]
  178.  
  179.  
  180. def commonprefix(m):
  181.     '''Given a list of pathnames, returns the longest common leading component'''
  182.     if not m:
  183.         return ''
  184.     
  185.     prefix = m[0]
  186.     for item in m:
  187.         for i in range(len(prefix)):
  188.             if prefix[:i + 1] != item[:i + 1]:
  189.                 prefix = prefix[:i]
  190.                 if i == 0:
  191.                     return ''
  192.                 
  193.                 break
  194.                 continue
  195.         
  196.     
  197.     return prefix
  198.  
  199.  
  200. def getsize(filename):
  201.     '''Return the size of a file, reported by os.stat()'''
  202.     return os.stat(filename).st_size
  203.  
  204.  
  205. def getmtime(filename):
  206.     '''Return the last modification time of a file, reported by os.stat()'''
  207.     return os.stat(filename).st_mtime
  208.  
  209.  
  210. def getatime(filename):
  211.     '''Return the last access time of a file, reported by os.stat()'''
  212.     return os.stat(filename).st_atime
  213.  
  214.  
  215. def getctime(filename):
  216.     '''Return the creation time of a file, reported by os.stat().'''
  217.     return os.stat(filename).st_ctime
  218.  
  219.  
  220. def islink(path):
  221.     '''Test for symbolic link.  On OS/2 always returns false'''
  222.     return False
  223.  
  224.  
  225. def exists(path):
  226.     '''Test whether a path exists'''
  227.     
  228.     try:
  229.         st = os.stat(path)
  230.     except os.error:
  231.         return False
  232.  
  233.     return True
  234.  
  235. lexists = exists
  236.  
  237. def isdir(path):
  238.     '''Test whether a path is a directory'''
  239.     
  240.     try:
  241.         st = os.stat(path)
  242.     except os.error:
  243.         return False
  244.  
  245.     return stat.S_ISDIR(st.st_mode)
  246.  
  247.  
  248. def isfile(path):
  249.     '''Test whether a path is a regular file'''
  250.     
  251.     try:
  252.         st = os.stat(path)
  253.     except os.error:
  254.         return False
  255.  
  256.     return stat.S_ISREG(st.st_mode)
  257.  
  258.  
  259. def ismount(path):
  260.     '''Test whether a path is a mount point (defined as root of drive)'''
  261.     (unc, rest) = splitunc(path)
  262.     if unc:
  263.         return rest in ('', '/', '\\')
  264.     
  265.     p = splitdrive(path)[1]
  266.     if len(p) == 1:
  267.         pass
  268.     return p[0] in '/\\'
  269.  
  270.  
  271. def walk(top, func, arg):
  272.     '''Directory tree walk whth callback function.
  273.  
  274.     walk(top, func, arg) calls func(arg, d, files) for each directory d
  275.     in the tree rooted at top (including top itself); files is a list
  276.     of all the files and subdirs in directory d.'''
  277.     
  278.     try:
  279.         names = os.listdir(top)
  280.     except os.error:
  281.         return None
  282.  
  283.     func(arg, top, names)
  284.     exceptions = ('.', '..')
  285.     for name in names:
  286.         if name not in exceptions:
  287.             name = join(top, name)
  288.             if isdir(name):
  289.                 walk(name, func, arg)
  290.             
  291.         isdir(name)
  292.     
  293.  
  294.  
  295. def expanduser(path):
  296.     '''Expand ~ and ~user constructs.
  297.  
  298.     If user or $HOME is unknown, do nothing.'''
  299.     if path[:1] != '~':
  300.         return path
  301.     
  302.     i = 1
  303.     n = len(path)
  304.     while i < n and path[i] not in '/\\':
  305.         i = i + 1
  306.     if i == 1:
  307.         if 'HOME' in os.environ:
  308.             userhome = os.environ['HOME']
  309.         elif 'HOMEPATH' not in os.environ:
  310.             return path
  311.         else:
  312.             
  313.             try:
  314.                 drive = os.environ['HOMEDRIVE']
  315.             except KeyError:
  316.                 drive = ''
  317.  
  318.             userhome = join(drive, os.environ['HOMEPATH'])
  319.     else:
  320.         return path
  321.     return userhome + path[i:]
  322.  
  323.  
  324. def expandvars(path):
  325.     '''Expand shell variables of form $var and ${var}.
  326.  
  327.     Unknown variables are left unchanged.'''
  328.     if '$' not in path:
  329.         return path
  330.     
  331.     import string
  332.     varchars = string.letters + string.digits + '_-'
  333.     res = ''
  334.     index = 0
  335.     pathlen = len(path)
  336.     while index < pathlen:
  337.         c = path[index]
  338.         if c == "'":
  339.             path = path[index + 1:]
  340.             pathlen = len(path)
  341.             
  342.             try:
  343.                 index = path.index("'")
  344.                 res = res + "'" + path[:index + 1]
  345.             except ValueError:
  346.                 res = res + path
  347.                 index = pathlen - 1
  348.             except:
  349.                 None<EXCEPTION MATCH>ValueError
  350.             
  351.  
  352.         None<EXCEPTION MATCH>ValueError
  353.         if c == '$':
  354.             if path[index + 1:index + 2] == '$':
  355.                 res = res + c
  356.                 index = index + 1
  357.             elif path[index + 1:index + 2] == '{':
  358.                 path = path[index + 2:]
  359.                 pathlen = len(path)
  360.                 
  361.                 try:
  362.                     index = path.index('}')
  363.                     var = path[:index]
  364.                     if var in os.environ:
  365.                         res = res + os.environ[var]
  366.                 except ValueError:
  367.                     res = res + path
  368.                     index = pathlen - 1
  369.                 except:
  370.                     None<EXCEPTION MATCH>ValueError
  371.                 
  372.  
  373.             None<EXCEPTION MATCH>ValueError
  374.             var = ''
  375.             index = index + 1
  376.             c = path[index:index + 1]
  377.             while c != '' and c in varchars:
  378.                 var = var + c
  379.                 index = index + 1
  380.                 c = path[index:index + 1]
  381.             if var in os.environ:
  382.                 res = res + os.environ[var]
  383.             
  384.             if c != '':
  385.                 res = res + c
  386.             
  387.         else:
  388.             res = res + c
  389.         index = index + 1
  390.     return res
  391.  
  392.  
  393. def normpath(path):
  394.     '''Normalize path, eliminating double slashes, etc.'''
  395.     path = path.replace('\\', '/')
  396.     (prefix, path) = splitdrive(path)
  397.     while path[:1] == '/':
  398.         prefix = prefix + '/'
  399.         path = path[1:]
  400.     comps = path.split('/')
  401.     i = 0
  402.     while i < len(comps):
  403.         if comps[i] == '.':
  404.             del comps[i]
  405.             continue
  406.         if comps[i] == '..' and i > 0 and comps[i - 1] not in ('', '..'):
  407.             del comps[i - 1:i + 1]
  408.             i = i - 1
  409.             continue
  410.         if comps[i] == '' and i > 0 and comps[i - 1] != '':
  411.             del comps[i]
  412.             continue
  413.         i = i + 1
  414.     if not prefix and not comps:
  415.         comps.append('.')
  416.     
  417.     return prefix + '/'.join(comps)
  418.  
  419.  
  420. def abspath(path):
  421.     '''Return the absolute version of a path'''
  422.     if not isabs(path):
  423.         path = join(os.getcwd(), path)
  424.     
  425.     return normpath(path)
  426.  
  427. realpath = abspath
  428. supports_unicode_filenames = False
  429.